#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef double db;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); }
	while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+ch-'0'; ch=getchar(); }
	return x*f;
}
const int MAXN=410,INF=0x40000000;
int n,v[MAXN],w[MAXN],f[MAXN][MAXN],g[MAXN][MAXN],h[MAXN][MAXN],ans[MAXN];
int main()
{
	n=read();
	for(int i=1;i<=n;++i)v[i]=read();
	for(int i=1;i<=n;++i)w[i]=read();
	for(int l=1;l<=n;++l)
		for(int i=1;i+l-1<=n;++i)
		{
			int j=i+l-1;
			if(i==j)
			{
				f[i][j]=v[1];
				g[i][j]=h[i][j]=0;
				continue;
			}
			g[i][j]=h[i][j]=-INF;
			for(int k=i+1;k<=j;++k)
			{
				if(w[k]==w[i]+1)g[i][j]=max(g[i][j],f[i+1][k-1]+g[k][j]);
				else if(w[k]==w[i]-1)h[i][j]=max(h[i][j],f[i+1][k-1]+h[k][j]);
			}
			f[i][j]=-INF;
			for(int k=i;k<j;++k)
				f[i][j]=max(f[i][j],f[i][k]+f[k+1][j]);
			for(int k=i;k<=j;++k)
				if(g[i][k]>-1<<20&&h[k][j]>-1<<20)	
					f[i][j]=max(f[i][j],g[i][k]+h[k][j]+v[w[k]*2-w[i]-w[j]+1]);
		}
	for(int i=1;i<=n;++i)
	{
		ans[i]=ans[i-1];
		for(int j=1;j<=i;++j)
			ans[i]=max(ans[i],ans[j-1]+f[j][i]);
	}
	printf("%d\n",ans[n]);
	return 0;
}
